<template>
  <el-form :model="userForm" ref="userFormRef" :rules="userFormRules" label-width="70px">
    <el-form-item>
      <UserAvatar @uploadAfter="uploadAfter"/>
    </el-form-item>
    <el-form-item label="用户名">
      {{ userForm.userName }}
    </el-form-item>
    <el-form-item label="昵称" prop="nickName">
      <el-input v-model="userForm.nickName"/>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio-group v-model="userForm.sex">
        <el-radio :label="item.dictValue" v-for="item in sys_user_sex" :key="item.dictValue"
                  v-show="item.dictLabel !== '未知'">{{ item.dictLabel }}
        </el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="手机号" prop="phonenumber">
      <el-input v-model="userForm.phonenumber"/>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="userForm.email"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit(userFormRef)">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import {Response} from "@/entity/response";
import {FormInstance} from 'element-plus';

const emit = defineEmits(["updateAfter", "uploadAfter"]);
const {userApi} = useApi();
const {msgSuccess, msgError} = useUtils().ModalUtils;
const {userInfo} = useStoreToRefs(useStores.user());
const {setUserInfo} = useStores.user();
const userForm = reactive({
  ...userInfo.value
});
const userFormRules = {
  nickName: [
    {required: true, message: '请输入昵称', trigger: 'blur'},
    {max: 10, message: '昵称的长度不能超过10', trigger: 'blur'},
  ],
  phonenumber: [
    {required: true, message: '请输入手机号', trigger: 'blur'},
    {
      validator: (rule: any, value: any, callback: any) => {
        if (useUtils().ValidateUtils.isPhone(value)) {
          callback();
        } else {
          callback(new Error('手机号格式不正确'));
        }
      },
      trigger: "blur"
    }
  ],
  email: [
    {required: true, message: '请输入邮箱号', trigger: 'blur'},
    {
      validator: (rule: any, value: any, callback: any) => {
        if (useUtils().ValidateUtils.isEmail(value)) {
          callback();
        } else {
          callback(new Error('邮箱格式不正确'));
        }
      },
      trigger: "blur"
    }
  ],
  sex: [
    {required: true, message: '请选择性别', trigger: 'change'},
  ]
};
const userFormRef = ref<FormInstance>();
const sys_user_sex = await useUtils().DictUtils.getDictList("sys_user_sex");
const updateUser = () => {
  userApi.updateUser(userForm).then((res: any) => {
    const {code}: Response = res;
    if (code === 200) {
      setUserInfo();
      emit("updateAfter");
      msgSuccess("修改成功");
    }
  });
};
const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      updateUser();
    } else {
      msgError('请填写完整信息');
    }
  });
}
const uploadAfter = () => {
  emit("uploadAfter");
}
</script>

<style scoped lang="scss">
</style>